package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.nursing.vo.TimeCountVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.aspectj.weaver.loadtime.Aj;
import org.hibernate.validator.constraintvalidators.RegexpURLValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/member/reservation")
@Tag(name = "预约管理")
public class ReservationController {
    @Autowired
    IReservationService service;

    @GetMapping("/cancelled-count")
    @Operation(description = "查询取消预约数量")
    public AjaxResult queryCancelled() {
        UserThreadLocal.getUserId();
        return AjaxResult.success(service.getCancelledCount(UserThreadLocal.getUserId()));
    }

    @GetMapping("/countByTime")
    @Operation(description = "查询每个时间段剩余预约次数")
    public AjaxResult queryCountByTime(@RequestParam("time") Long time) {
        List<TimeCountVo> timeCountVos = service.countReservationsForTime(time);
        return AjaxResult.success(timeCountVos);
    }

    @Operation(description = "新增预约")
    @PostMapping
    public AjaxResult insert(@RequestBody ReservationDto reservationDto) {
        return AjaxResult.success(service.insertReservation(reservationDto));
    }

    @GetMapping("/page")
    public AjaxResult selectByPage(@RequestParam(required = false) int pageNum, @RequestParam(required = false) int pageSize, @RequestParam(required = false) Integer status) {
        TableDataInfo<Reservation> reservationTableDataInfo = service.selectByPage(pageNum, pageSize, status);
        return AjaxResult.success(reservationTableDataInfo);
    }

    @PutMapping("/{id}/cancel")
    public AjaxResult cancel(@PathVariable Long id) {
        int i = service.cancelReservation(id);
        return AjaxResult.success(i);
    }
}
